package cn.qyd.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author 邱运铎
 * 模拟并行计算处理
 * @date 2024-03-23 0:15
 */
public class ParallelCalculation {
    private final static int NTHREADS = 4;
    private final static ExecutorService pool = Executors.newFixedThreadPool(NTHREADS);

    public static void main(String[] args) {
        Callable<Double> task = new Callable() {
            @Override
            public Double call() {
                //模拟计算处理结果
                return Math.random()*100;
            }
        };

        List<Future<Double>> result = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            result.add(pool.submit(task));
        }

        for (Future<Double> future : result) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        pool.shutdown();
    }
}
